package com.content.view.Frame.MainPanel
{
	import com.content.model.Const.TransitionType;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	import com.library.utils.Geometry;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.errors.IllegalOperationError;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	
	public class MPBase extends UIObject implements IDisposable
	{
		public static const NAME:String						= 'MPBase';
		public static const SWITCH:String					= NAME + '-' + 'Switch';
		public static const ON_SHOWN:String					= NAME + '-' + 'OnShown';
		
		private var _bg:Shape;
		private var _mask:Shape;
		private var _bgSkin:Sprite;
		protected var _container:Sprite;
		
		private var _marginTop:int;
		private var _marginBottom:int;
		private var _marginLeft:int;
		private var _marginRight:int;
		private var _cBarWidth:int;
		
		public function MPBase(
								self:MPBase,
								marginTop:int,
								marginBottom:int,
								marginLeft:int,
								marginRight:int,
								cBarWidth:int, 
								bgSkin:Sprite = null)
		{
			_marginTop = marginTop;
			_marginBottom = marginBottom;
			_marginLeft = marginLeft;
			_marginRight = marginRight;
			_cBarWidth = cBarWidth;
			
			if(self != this){				
				throw new IllegalOperationError("错误: MPBase 类不能被实例化！");
			}
			
			if(bgSkin != null){
				_bgSkin = bgSkin;
			}
			
			_container = new Sprite();
			addChild(_container);
			
			_mask = new Shape();
			addChild(_mask);
			_container.mask = _mask;
			
			if(_bgSkin != null){
				_container.addChild(_bgSkin);
			}else{
				_bg = new Shape();
				_container.addChild(_bg);
			}
			
			this.alpha = 0;
			this.visible = false;
		}
		
		// Event
		private function OnShown():void{
			dispatchEvent(new DataEvent(ON_SHOWN, false, false));
		}
		
		// 此函数由子类重写
		protected function SetLayoutMP(areaWidth:int, areaHeight:int):void{}
		
		/* Public Method
		* ---------------------------------------------------- */
		public function SetLayout(sWidth:int, sHeight:int, ellipse:Number = 20):void{
			var bgWidth:int = sWidth - _marginLeft - _marginRight - _cBarWidth;
			var bgHeight:int = sHeight - _marginTop - _marginBottom;
			
			if(_bgSkin != null){
				_bgSkin.width = bgWidth;
				_bgSkin.height = bgHeight;
			}else{
				_bg.graphics.clear();
				_bg.graphics.beginFill(0xebebeb,1.0);
				_bg.graphics.lineStyle(1, 0xf8f8f8,1.0);
				_bg.graphics.drawRoundRect(0, 0, bgWidth, bgHeight, 0, 0);
			}
			
			_mask.graphics.clear();
			_mask.graphics.beginFill(0xff0000, 1.0);
			_mask.x = int(bgWidth/2);
			_mask.y = int(bgHeight/2);
			_mask.graphics.drawRoundRect(-int(bgWidth/2),-int(bgHeight/2), bgWidth, bgHeight, ellipse, ellipse);
			
			SetLayoutMP(bgWidth, bgHeight);
		}
				
		public function Show(delay:Number = 0, duration:Number = 0.5):void{
			_mask.scaleX = 1;
			_mask.scaleY = 0;
			TweenLite.to( this, duration, { autoAlpha: 1, delay: delay, onComplete: OnShown } );
			TweenLite.to( _mask, duration, { scaleY: 1, delay: delay, ease:Cubic.easeInOut} );
		}		
		public function Hide(delay:Number = 0, duration:Number = 0.5):void	{			
			TweenLite.to( this, duration, { autoAlpha: 0, delay: delay } );
			TweenLite.to( _mask, duration, { scaleY: 0, delay: delay} );
		}		
		public function Dispose():void{}
	}
}